﻿using SCM.Common;
using SCM.Common.Extensions;
using SCM.Model;
using SCM.Model.DBEntity;
using SCM.Servie.IBusiness;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace SCM.Servie.Business 
{
    /// <summary>
    /// 产品豁免条款服务
    /// </summary>
    public class MaterialProductExemptionService : IMaterialProductExemptionService
    {
        private readonly DbContext dal = new DbContext();

        public string Add(T_Material_ProductExemption model)
        {
            return dal.Insertable(model).ExecuteReturnEntity().ID;
        }

        public bool Delete(List<string> ids)
        {
            return dal.Deleteable<T_Material_ProductExemption>(p => ids.Contains(p.ID)).ExecuteCommand() > 0;
        }

        public T_Material_ProductExemption GetById(string id)
        {
            return dal.Get<T_Material_ProductExemption>(id);
        }

        public List<T_Material_ProductExemption> GetList(string materialProductID)
        {
            Expression<Func<T_Material_ProductExemption, bool>> whereExp = (a) => true;
            if (!string.IsNullOrWhiteSpace(materialProductID))
            {
                whereExp = whereExp.Compose((a) => a.MaterialProductID.StartsWith(materialProductID), Expression.AndAlso);
            }
            return dal.Queryable<T_Material_ProductExemption>().Where(whereExp).ToList();
        }

        public PageData<T_Material_ProductExemption> Query(string materialProductID, PageInfo page)
        {
            int count = 0;
            Expression<Func<T_Material_ProductExemption, bool>> whereExp = (a) => true;
            if (!string.IsNullOrWhiteSpace(materialProductID))
            {
                whereExp = whereExp.Compose((a) => a.MaterialProductID.StartsWith(materialProductID), Expression.AndAlso);
            }
            List<T_Material_ProductExemption> list = dal.Queryable<T_Material_ProductExemption>().Where(whereExp).ToPageList(page.PageIndex, page.PageSize, ref count);
            page.DataCount = count;
            return new PageData<T_Material_ProductExemption> { Data = list, Page = page };
        }

        public bool Update(T_Material_ProductExemption model)
        {
            return dal.Modify(model);
        }
    }
}
